package codetrainning.test5.studentproject.dao;





import codetrainning.test5.studentproject.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao extends BaseStudentDao{
    private static ArrayList<Student> arr = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001","张三","23","1999-01-01");
        Student stu2 = new Student("heima002","李四","24","2000-01-01");

        arr.add(stu1);
        arr.add(stu2);
    }

    @Override
    public boolean addStudent(Student student){
        arr.add(student);
        return true;
    }

    @Override
    public Student[] findAll() {
        Student[] students = new Student[arr.size()];

        for (int i = 0; i < arr.size(); i++) {
            students[i] = arr.get(i);
        }

        return students;
    }
    @Override
    public int isExist(String id) {
        //调用dao，拿到所有容器数据
        int index = -1;
        for (int i = 0; i < arr.size(); i++) {
            Student stu = arr.get(i);
            if(id.equals(stu.getId())){
                index = i;
                break;

            }
        }
        return index;
    }

    @Override
    public boolean updateStudent(Student stu) {
        int index = isExist(stu.getId());
        arr.set(index,stu);
        return true;
    }

    @Override
    public boolean deleteStudent(int index) {
        arr.remove(index);
        return true;
    }
}
